package com.metagurukul.metagranth.service;

import java.sql.SQLException;
import java.util.ArrayList;

import junit.framework.TestCase;

import com.metagurukul.metagranth.vo.Answer;
import com.metagurukul.metagranth.vo.MetaData;
import com.metagurukul.metagranth.vo.Question;
import com.metagurukul.metagranth.vo.QuestionTag;
import com.metagurukul.metagranth.vo.Tag;

/*
 * this class is a jUnit class testing QuestionService class
 */
public class QuestionJUnitTEST extends TestCase {

	/**
	 * this function test the QuestionService class about null check of
	 * AskQuestion function(the question and tag given to functiion is null )
	 * 
	 * secnario:database is having some questions already and we add 1 dummy
	 * question here which is null and 1 tag which is also null
	 * 
	 * expected result:function should return error code of null question or tag
	 * actual result :function returns 0
	 * 
	 * @author rohit
	 * 
	 */
	public void testAskQuestionNullCheck() {
		int testUserId = -1;
		try {// create a test user
			testUserId = CommonJUnit.createTestUser();
			// question object
			Question question = null;
			Tag tag = null;
			// call service class method
			int result = QuestionService.askQuestion(question, tag);
			assertEquals(result, 0);// status 0 means error

		} catch (Exception e) {
			fail();
			e.printStackTrace();
		} finally {

			try {
				CommonJUnit.deleteTestDataFromQuestion_Tags(testUserId);
				CommonJUnit.deleteTestData("METACUBEGRANTH_QUESTION",testUserId);
				CommonJUnit.deleteTestUser();
			} catch (SQLException e) {
				fail();
				e.printStackTrace();
			}

			// CommonJUnit.deleteTestDataFromTags(tagName);
		}

	}

	/**
	 * this function test the QuestionService class about null check of
	 * AskQuestion function(the question and tag given to functiion is null )
	 * 
	 * secnario:database is having some questions already and we add 1 dummy
	 * question here which is null and i tag which is also null
	 * 
	 * expected result:function should return error code of null question or tag
	 * actual result :function returns 1
	 * 
	 * @author rohit
	 * 
	 */
	public void testAskQuestionValidQuestionRowInserted() {
		int testUserId = -1;
		String tagName = "test";
		try {// create a test user
			testUserId = CommonJUnit.createTestUser();
			// create a test question
			Question question = new Question();
			question.setQuestion("testtesttest");
			question.setTitle("test");
			question.setUserId(testUserId);
			// create a test tag
			Tag tag = new Tag();

			tag.setTagName(tagName);
			// call service class method
			int result = QuestionService.askQuestion(question, tag);
			assertEquals(result, 1);// status 1 means success

		} catch (Exception e) {
			e.printStackTrace();
			fail();
		} finally {
			try {
				CommonJUnit.deleteTestDataFromQuestion_Tags(testUserId);
				CommonJUnit.deleteTestData("METACUBEGRANTH_QUESTION",
						testUserId);
				CommonJUnit.deleteTestDataFromTags(tagName);
				CommonJUnit.deleteTestUser();
			} catch (SQLException e) {
				fail();
				e.printStackTrace();
			}

		}

	}

	/**
	 * this function test the QuestionService class about changes made in all
	 * relevant tables of AskQuestion function (the question and tag given to
	 * function are valid )
	 * 
	 * secnario:database is having some questions already and we add 1 dummy
	 * question here which is not null and 1 tag which is also not null
	 * 
	 * expected result:function should do all the changes in all relevant
	 * tables. actual result :there should be changes in database.
	 * 
	 * @author rohit
	 * 
	 */
	public void testAskQuestionValidQuestionTableUpdated() {
		int testUserId = -1;
		String tagName = "testTag";
		try {// create a test user
			testUserId = CommonJUnit.createTestUser();
			// create a test question
			Question question = new Question();
			question.setQuestion("testtesttest");
			question.setTitle("testTitle");
			question.setUserId(testUserId);
			// create a test tag
			Tag tag = new Tag();
			tag.setTagName(tagName);
			// retrieve questionlist for test user id.
			ArrayList<Question> questionList = CommonJUnit
					.retrieveTestQuestions(testUserId);
			// retrieve tag list for tag name
			ArrayList<Tag> tagList = CommonJUnit.retrieveTestTag(tagName);
			// retrieve question tag list for test user
			ArrayList<QuestionTag> questionTagList = CommonJUnit
					.retrieveTestQuestion_Tag(testUserId);
			// all list should be blank
			assertEquals(questionList.size(), 0);
			assertEquals(tagList.size(), 0);
			assertEquals(questionTagList.size(), 0);
			// call service class method
			QuestionService.askQuestion(question, tag);
			// again retrieve data from database
			questionList = CommonJUnit.retrieveTestQuestions(testUserId);
			tagList = CommonJUnit.retrieveTestTag(tagName);
			questionTagList = CommonJUnit.retrieveTestQuestion_Tag(testUserId);
			// there should be 1 row change in all three tables
			assertEquals(questionList.size(), 1);
			assertEquals(tagList.size(), 1);
			assertEquals(questionTagList.size(), 1);
			// mapping at question table
			Question savedQuestion = questionList.get(0);
			// both the questions should be same
			assertEquals(savedQuestion.getIsClosed(), question.getIsClosed());
			assertEquals(savedQuestion.getQuestion(), question.getQuestion());
			assertEquals(savedQuestion.getTitle(), question.getTitle());
			assertEquals(savedQuestion.getUserId(), question.getUserId());
			// mapping at tag table
			Tag savedTag = tagList.get(0);
			assertEquals(savedTag.getTagName(), tag.getTagName());
			// mapping at question_tag
			QuestionTag qt = questionTagList.get(0);
			assertEquals(qt.getQid(), savedQuestion.getQuesId());
			assertEquals(qt.getTagid(), savedTag.getTagid());

		} catch (Exception e) {
			e.printStackTrace();
			fail();
		} finally {

			try {
				CommonJUnit.deleteTestDataFromQuestion_Tags(testUserId);
				CommonJUnit.deleteTestData("METACUBEGRANTH_QUESTION",
						testUserId);
				CommonJUnit.deleteTestDataFromTags(tagName);
				CommonJUnit.deleteTestUser();
			} catch (SQLException e) {
				fail();
				e.printStackTrace();
			}

		}

	}

	/**
	 * this function test the QuestionService class about positive case of
	 * getLatestQuestions function(the questions are present in the databases
	 * more than one and it returns the arraylist of metadata of question)
	 * 
	 * secnario:database is having some questions already and we add 1 dummy
	 * question here first
	 * 
	 * expected result:function should return added questions on the top. actual
	 * result :function returns true .
	 * 
	 * @author rohit
	 * 
	 */

	public void testGetLatestQuestionsPositiveCase() { // test data
		int testUserId = -1;
		try {
			// create a test user
			testUserId = CommonJUnit.createTestUser();
			// create a new question
			Question newQuestion = new Question();
			newQuestion.setQuestion("testtesttest");
			newQuestion.setTitle("test");
			newQuestion.setUserId(testUserId);
			// create a new tag
			Tag newTag = new Tag();
			newTag.setTagName("testtag");
			// add question to database
			QuestionService.askQuestion(newQuestion, newTag);
			// call service class method
			ArrayList<MetaData> metaList = QuestionService.getLatestQuestions();
			MetaData firstQuestion = metaList.get(0);
			// check question
			assertTrue(firstQuestion.getQuestion().getQuestion()
					.equals(newQuestion.getQuestion()));
			// check tag
			assertTrue(firstQuestion.getTagList().get(0).getTagName()
					.equals(newTag.getTagName()));
		} catch (Exception e) {
			fail();
			e.printStackTrace();
		} finally {
			try {
				CommonJUnit.deleteTestDataFromQuestion_Tags(testUserId);
				CommonJUnit.deleteTestData("METACUBEGRANTH_QUESTION",
						testUserId);
				CommonJUnit.deleteTestDataFromTags("testtag");
				CommonJUnit.deleteTestUser();
			} catch (SQLException e) {
				fail();
				e.printStackTrace();
			}

		}
	}

	/**
	 * this function test the QuestionService class about positive case of
	 * searchByQuestion function(the questions are present in the databases more
	 * than one and it returns the arraylist of metadata of question)
	 * 
	 * secnario:database is having some questions already and we add 1 dummy
	 * question here first
	 * 
	 * expected result:function should return added questions on the top actual
	 * result :function returns true and we check if the retrieved data is same.
	 * 
	 * @author rohit
	 * 
	 */

	public void testSearchByQuestionPositiveCase() {
		int testUserId = -1;
		try {
			// create a test user
			testUserId = CommonJUnit.createTestUser();
			// create a new question
			Question newQuestion = new Question();
			newQuestion.setQuestion("JUnittesttesttest");
			newQuestion.setTitle("JUnittest");
			newQuestion.setUserId(testUserId);
			Tag newTag = new Tag();
			newTag.setTagName("JUnittesttag");
			// add question to database
			// call service class method
			QuestionService.askQuestion(newQuestion, newTag);
			ArrayList<MetaData> metaList = QuestionService.searchByQuestion("JUnittest");
			MetaData firstQuestion = metaList.get(0);
			// check question
			assertTrue(firstQuestion.getQuestion().getQuestion().equals(newQuestion.getQuestion()));
			// check tag
			assertTrue(firstQuestion.getTagList().get(0).getTagName()
					.equals(newTag.getTagName()));
		} catch (Exception e) {
			fail();
			e.printStackTrace();
		} finally {
			try {
				CommonJUnit.deleteTestDataFromQuestion_Tags(testUserId);
				CommonJUnit.deleteTestData("METACUBEGRANTH_QUESTION",testUserId);
				CommonJUnit.deleteTestDataFromTags("JUnittesttag");
				CommonJUnit.deleteTestUser();

			} catch (SQLException e) {
				fail();
				e.printStackTrace();
			}

		}
	}

	/**
	 * this function test the QuestionService class about positive case of
	 * searchByTag function(the questions are present in the databases more than
	 * one and it returns the arraylist of metadata of question)
	 * 
	 * secnario:database is having some questions already and we add 1 dummy
	 * question here first
	 * 
	 * expected result:function should return added questions on the top. actual
	 * result :function returns true.we retrieve the question and check is it
	 * right added question.
	 * 
	 * @author rohit
	 * 
	 */

	public void testSearchByTagPositiveCase() { // test data
		int testUserId = -1;
		try {
			// create a test user
			testUserId = CommonJUnit.createTestUser();
			// create a question
			Question newQuestion = new Question();
			newQuestion.setQuestion("testtesttest");
			newQuestion.setTitle("test");
			newQuestion.setUserId(testUserId);
			// create a new tag
			Tag newTag = new Tag();
			newTag.setTagName("testtag");
			// add question to database
			// call service class method which is already tested
			QuestionService.askQuestion(newQuestion, newTag);
			// call service class method
			ArrayList<MetaData> metaList = QuestionService
					.searchByTag("testtag");
			MetaData firstQuestion = metaList.get(0);
			// check questioon
			assertTrue(firstQuestion.getQuestion().getQuestion()
					.equals(newQuestion.getQuestion()));
			// check tag
			assertTrue(firstQuestion.getTagList().get(0).getTagName()
					.equals(newTag.getTagName()));
		} catch (Exception e) {
			fail();
			e.printStackTrace();
		} finally {
			try {
				CommonJUnit.deleteTestDataFromQuestion_Tags(testUserId);
				CommonJUnit.deleteTestData("METACUBEGRANTH_QUESTION",
						testUserId);
				CommonJUnit.deleteTestDataFromTags("testtag");
				CommonJUnit.deleteTestUser();

			} catch (SQLException e) {
				fail();
				e.printStackTrace();
			}

		}
	}

	/**
	 * this function test the QuestionService class about positive case of
	 * searchByQuestionAndTag function(the questions are present in the
	 * databases more than one and it returns the arraylist of metadata of
	 * question)
	 * 
	 * secnario:database is having some questions already and we add 1 dummy
	 * question here first
	 * 
	 * expected result:function should return added questions on the top actual
	 * result :function returns true and we retrieve data to check is if it is
	 * posted question.
	 * 
	 * @author rohit
	 * 
	 */

	public void testSearchByQuestionAndTagPositiveCase() { // test data
		int testUserId = -1;
		try {
			// create a test user
			testUserId = CommonJUnit.createTestUser();
			// create a new question
			Question newQuestion = new Question();
			newQuestion.setQuestion("testtesttest");
			newQuestion.setTitle("test");
			newQuestion.setUserId(testUserId);
			// create a new tag
			Tag newTag = new Tag();
			newTag.setTagName("testtag");
			// add question to database
			// call service class method
			QuestionService.askQuestion(newQuestion, newTag);
			ArrayList<MetaData> metaList = QuestionService
					.searchByQuestionAndTag("test", "testtag");
			MetaData firstQuestion = metaList.get(0);
			// check questioon
			assertTrue(firstQuestion.getQuestion().getQuestion()
					.equals(newQuestion.getQuestion()));
			// check tag
			assertTrue(firstQuestion.getTagList().get(0).getTagName()
					.equals(newTag.getTagName()));
		} catch (Exception e) {
			e.printStackTrace();
			fail();
		} finally {
			try {
				CommonJUnit.deleteTestDataFromQuestion_Tags(testUserId);
				CommonJUnit.deleteTestData("METACUBEGRANTH_QUESTION",
						testUserId);
				CommonJUnit.deleteTestDataFromTags("testtag");
				CommonJUnit.deleteTestUser();

			} catch (SQLException e) {
				fail();
				e.printStackTrace();
			}

		}
	}

	/**
	 * this function test the QuestionService class about positive case of
	 * GetQuestionDetailByQuestionId function(the questions are present in the
	 * databases more than one and it returns metadata of question)
	 * 
	 * secnario:database is having some questions already and we add 1 dummy
	 * question here first.then we give a question id and it will get the same
	 * question.
	 * 
	 * expected result:function should return added questions . actual result
	 * :function return added qustion.
	 * 
	 * @author rohit
	 * 
	 */

	public void testGetQuestionDetailByQuestionIdPositiveCase() { // test data
		int testUserId = -1;
		try {
			// create a test user
			testUserId = CommonJUnit.createTestUser();
			Question newQuestion = new Question();
			newQuestion.setQuestion("testtesttest");
			newQuestion.setTitle("test");
			newQuestion.setUserId(testUserId);
			// add question to database
			// call service class method
			int questionId = CommonJUnit.insertTestData(newQuestion);
			MetaData metaDataOfQuestion = QuestionService
					.getQuestionDetailByQuestionId(questionId);
			// check questioon
			assertTrue(metaDataOfQuestion.getQuestion().getQuestion()
					.equals(newQuestion.getQuestion()));
		} catch (Exception e) {
			fail();
			e.printStackTrace();
		} finally {
			try {
				CommonJUnit.deleteTestDataFromQuestion_Tags(testUserId);
				CommonJUnit.deleteTestData("METACUBEGRANTH_QUESTION",
						testUserId);
				CommonJUnit.deleteTestDataFromTags("testtag");
				CommonJUnit.deleteTestUser();
			} catch (SQLException e) {
				fail();
				e.printStackTrace();
			}

		}
	}
	
	
	/**
	 * this function test the QuestionService class about positive case of
	 * getAllQuestions function(the questions are present in the
	 * databases more than one and it returns metadata of question)
	 * 
	 * secnario:database is having some questions already and we add 1 dummy
	 * question here first.The count increases after adding question.
	 * 
	 * expected result:function should return arraylist with one more size.
	 *  actual result:function return 1  more size of arraylist.
	 * 
	 * @author metagranth team
	 * 
	 */

	public void testGetAllQuestionsPositiveCase() { // test data
		int testUserId = -1;
		try {
			// create a test user
			testUserId = CommonJUnit.createTestUser();
			// create a question
			Question newQuestion = new Question();
			newQuestion.setQuestion("testtesttest");
			newQuestion.setTitle("test");
			newQuestion.setUserId(testUserId);
			// service class method call
			ArrayList<MetaData> metaDataOfQuestionList = QuestionService
			.getAllQuestions();
			// take count
			int preSize = metaDataOfQuestionList.size();
			// add question to database
			// call service class method
			int questionId = CommonJUnit.insertTestData(newQuestion);
			// service class method call
			metaDataOfQuestionList = QuestionService.getAllQuestions();
			// after posting count
			int postSize = metaDataOfQuestionList.size();
			// count should be 1 more than previous.
			assertEquals(1, postSize - preSize);
		} catch (Exception e) {
			fail();
			e.printStackTrace();
		} finally {
			try {
				CommonJUnit.deleteTestDataFromQuestion_Tags(testUserId);
				CommonJUnit.deleteTestData("METACUBEGRANTH_QUESTION",
						testUserId);
				CommonJUnit.deleteTestDataFromTags("testtag");
				CommonJUnit.deleteTestUser();
			} catch (SQLException e) {
				fail();
				e.printStackTrace();
			}

		}
	}
	

	/**
	 * this function test the QuestionService class about positive case of
	 * getAllQuestionsForAdmin function(the questions are present in the
	 * databases more than one and it returns metadata of question which are not closed till 
	 * now and which hava at least one answer.)
	 * 
	 * secnario:database is having some questions already and we add 1 dummy
	 * question here first.The size of arraylist which function returns should be 1 less than 
	 * size of list after posting question.After posting answer size will be decreased again.
	 * 
	 * expected result:function should return valid list which should have size which increases or 
	 * decreases as any new question is posted or any answer is posted for any question. 
	 * actual result:we get size before question posting and after question posting,difference should be
	 * 1.again after posting answer difference should be 0.
	 * 
	 * @author rohit
	 * 
	 */

	public void testGetAllQuestionsForAdminPositiveCase() { // test data
		int testUserId = -1;
		try {
			// create a test user
			testUserId = CommonJUnit.createTestAdminUser();
			// create a question
			Question newQuestion = new Question();
			newQuestion.setQuestion("testtesttest");
			newQuestion.setTitle("test");
			newQuestion.setUserId(testUserId);
			// get meta data list of all qustions for admin
			ArrayList<MetaData> metaDataOfQuestionList = QuestionService
			.getAllQuestionsForAdmin();
			// get size of list
			int preSize = metaDataOfQuestionList.size();
			// add question to database
			int questionId = CommonJUnit.insertTestData(newQuestion);
			// again get list
			metaDataOfQuestionList = QuestionService.getAllQuestionsForAdmin();
			// size of list
			int postSize = metaDataOfQuestionList.size();
			// differnce should be 1 as 1 new posted question
			assertEquals(0, postSize - preSize);
			// make a new answer.
			Answer answer = new Answer();
			answer.setQid(questionId);
			answer.setAnswer("test answer");
			answer.setUid(testUserId);
			// post answer for qustion.
			AnswerService.saveAnswer(answer);
			// get meta data list
			metaDataOfQuestionList = QuestionService.getAllQuestionsForAdmin();
			// get size
			postSize = metaDataOfQuestionList.size();
			// it should be same as question posted is answered so it will not
			// be in list.
			assertEquals(1, postSize - preSize);

		} catch (Exception e) {
			e.printStackTrace();
			fail();
		} finally {
			try {
				CommonJUnit.deleteTestDataFromQuestion_Tags(testUserId);
				CommonJUnit.deleteTestData("METACUBEGRANTH_QUESTION",
						testUserId);
				CommonJUnit.deleteTestDataFromTags("testtag");
				CommonJUnit.deleteTestUser();
			} catch (SQLException e) {
				fail();
				e.printStackTrace();
			}

		}
	}


	/**
	 * this function test the QuestionService class about positive case of
	 * getUnansweredQuestions function(the questions are present in the
	 * databases more than one and it returns all unanswered questions)
	 * 
	 * secnario:database is having some questions already and we add 1 dummy
	 * question here first.The size of arraylist which function returns should be 1 less than 
	 * size of list after posting question.After posting answer size will be decreased again.
	 * 
	 * expected result:function should return valid list which should have size which increases or 
	 * decreases as any new question is posted or any answer is posted for any question. 
	 * actual result:we get size before question posting and after question posting,difference should be
	 * 1.again after posting answer difference should be 0.
	 * 
	 * @author rohit
	 */

	public void testGetUnansweredQuestionsPositiveCase() { // test data
		int testUserId = -1;
		try {
			// create a test user
			testUserId = CommonJUnit.createTestUser();
			// create a question
			Question newQuestion = new Question();
			newQuestion.setQuestion("testtesttest");
			newQuestion.setTitle("test");
			newQuestion.setUserId(testUserId);
			// get meta data list of all qustions
			ArrayList<MetaData> unAnswerwdQuestions=QuestionService.getUnansweredQuestions();
			// get size of list
			int preSize=unAnswerwdQuestions.size();
			// add question to database
			int questionId = CommonJUnit.insertTestData(newQuestion);
			// again get list
			unAnswerwdQuestions=QuestionService.getUnansweredQuestions();
			// size of list
			int postSize=unAnswerwdQuestions.size();
			// differnce should be 1 as 1 new posted question
			assertEquals(1, postSize-preSize);
			// make a new answer.
			Answer answer=new Answer();
			answer.setQid(questionId);
			answer.setAnswer("test answer");
			answer.setUid(testUserId);
			// post answer for qustion.
			AnswerService.saveAnswer(answer);
			// get meta data list
			unAnswerwdQuestions=QuestionService.getUnansweredQuestions();
			 postSize =unAnswerwdQuestions.size();
			// it should be same as question posted is answered so it will not
				// be in list.
			 assertEquals(0, postSize-preSize);
		} catch (Exception e) {
			e.printStackTrace();
			fail();
			
		} finally {
			try {
				CommonJUnit.deleteTestDataFromQuestion_Tags(testUserId);
				CommonJUnit.deleteTestData("METACUBEGRANTH_QUESTION",
						testUserId);
				CommonJUnit.deleteTestDataFromTags("testtag");
				CommonJUnit.deleteTestUser();
			} catch (SQLException e) {
				fail();
				e.printStackTrace();
			}

		}
	}
	/**
	 * this function test the QuestionService class about positive case of
	 * getQuestionTitleList function(the questions are present in the
	 * databases more than one and it returns all questions which have name like string given in argument.)
	 * 
	 * 
	 * secnario:database is having some questions already and we add 1 dummy
	 * question here first.The size of arraylist which function returns should be 1 less than 
	 * size of list after posting question.After deleting question size will be decreased again.
	 * 
	 * expected result:function should return valid list which should have size which increases or 
	 * decreases as any new question is posted or deleted . 
	 * actual result:we get size before question posting and after question posting,difference should be
	 * 1.again after deleting question difference should be 0.
	 * 
	 * @author rohit
	 */

	public void testGetQuestionTitleListPositiveCase() { // test data
		int testUserId = -1;
		try {
			// create a test user
			testUserId = CommonJUnit.createTestUser();
			// create a question
			Question newQuestion = new Question();
			newQuestion.setQuestion("testtesttest");
			newQuestion.setTitle("test");
			newQuestion.setUserId(testUserId);
			// get list of all question string
			ArrayList<String> list = (ArrayList<String>) QuestionService
			.getQuestionTitleList("test");
			// get size of list
			int preSize = list.size();
			// add question to database
			int questionId = CommonJUnit.insertTestData(newQuestion);
			// again get list
			// get list of all question string
			list = (ArrayList<String>) QuestionService
			.getQuestionTitleList("test");
			int postSize = list.size();
			// differnce should be 1 as 1 new posted question
			assertEquals(1, postSize - preSize);
			CommonJUnit.deleteTestData("METACUBEGRANTH_QUESTION", testUserId);
			// get data list
			list = (ArrayList<String>) QuestionService
			.getQuestionTitleList("test");
			postSize = list.size();
			// it should be same as question posted is answered so it will not
			// be in list.
			assertEquals(0, postSize - preSize);
		} catch (Exception e) {
			fail();
			e.printStackTrace();
		} finally {
			try {
				CommonJUnit.deleteTestDataFromQuestion_Tags(testUserId);
				CommonJUnit.deleteTestData("METACUBEGRANTH_QUESTION",
						testUserId);
				CommonJUnit.deleteTestDataFromTags("testtag");
				CommonJUnit.deleteTestUser();
			} catch (SQLException e) {
				fail();
				e.printStackTrace();
			}

		}
	}

	/**
	 * this function test the QuestionService class about positive case of
	 * updateEditedQuestion function(the questions are present in the
	 * databases more than one and it will update any questions properties if it is present(question id).)
	 * 
	 * 
	 * secnario:database is having some questions already and we add 1 dummy
	 * question here first.than we change question by service class method and test question.
	 *  
	 * expected result:function should update questions
	 * 
	 * @author rohit
	 */

	public void testUpdateEditedQuestionPositiveCase() { // test data
		int testUserId = -1;
		try {
			// create a test user
			testUserId = CommonJUnit.createTestUser();
			// create a question
			Question newQuestion = new Question();
			newQuestion.setQuestion("testtesttest");
			newQuestion.setTitle("test");
			newQuestion.setUserId(testUserId);
			// add question to database
			int questionId = CommonJUnit.insertTestData(newQuestion);
			// make a new question with updates(description is updated.)
			Question latestQuestion = new Question();
			latestQuestion.setQuestion("upated question");
			latestQuestion.setTitle("test");
			latestQuestion.setUserId(testUserId);
			latestQuestion.setQuesId(questionId);
			// upated question
			QuestionService.updateEditedQuestion(latestQuestion);
			// retrieve test data
			ArrayList<Question> questionList = CommonJUnit
			.retrieveTestQuestions(testUserId);
			// retrieve question
			Question retrievedQuestion = questionList.get(0);
			// description should be updated.
			assertTrue(latestQuestion.getQuestion().equals(
					retrievedQuestion.getQuestion()));
		} catch (Exception e) {
			fail();
			e.printStackTrace();
		} finally {
			try {
				CommonJUnit.deleteTestDataFromQuestion_Tags(testUserId);
				CommonJUnit.deleteTestData("METACUBEGRANTH_QUESTION",
						testUserId);
				CommonJUnit.deleteTestDataFromTags("testtag");
				CommonJUnit.deleteTestUser();
			} catch (SQLException e) {
				fail();
				e.printStackTrace();
			}

		}
	}

	/**
	 * this function test the QuestionService class about positive case of
	 * updateEditedTags function(the tags are present in the
	 * databases more than one and it will update any tags properties for given question id. 
	 * 
	 * secnario:database is having some tags already and we add 1 dummy
	 * question with tags here first.than we update tags for that question by service class method and test 
	 * retrieved tags if they have been updated.
	 *  
	 * expected result:function should update question tag mapping in question_tag table but not delete the tags.
	 * 
	 * @author rohit
	 */
	/*

	public void testUpdateEditedTagsPositiveCase() { // test data
		ConnectionPoolFactory cp = ConnectionPoolFactory.getInstance();
		cp.init();
		int testUserId = -1;
		try {
			// create a test user
			testUserId = CommonJUnit.createTestUser();
			// create a question
			Question newQuestion = new Question();
			newQuestion.setQuestion("testtesttest");
			newQuestion.setTitle("test");
			newQuestion.setUserId(testUserId);
			//create a tag
			Tag tag=new Tag();
			tag.setTagName("testTag");
			// add question to database with tags(mapping in question tag table.)
			int questionId = QuestionService.askQuestion(newQuestion, tag);
			// make a new tag with updates(tag list id updated two tags with comma sepretion is updated.)
			Tag newTag=new Tag();
			newTag.setTagName("testTag1,testTag2");
			QuestionService.updateEditedTags(newTag.getTagName(), questionId);
		
		} catch (Exception e) {
			fail();
			e.printStackTrace();
		} finally {
			try {
				CommonJUnit.deleteTestDataFromQuestion_Tags(testUserId);
				CommonJUnit.deleteTestData("METACUBEGRANTH_QUESTION",
						testUserId);
				CommonJUnit.deleteTestDataFromTags("testtag");
				CommonJUnit.deleteTestUser();
			} catch (SQLException e) {
				fail();
				e.printStackTrace();
			}

		}
	}

	*/
	
	
	/**
	 * this function test the QuestionService class about positive case of
	 * getQuestionById function(the tags are present in the
	 * databases more than one and it will return added question by its id. 
	 * 
	 * secnario:database is having some questions already and we add 1 dummy
	 * question.then  it will be retrieved by its question id.
	 * .
	 *  
	 * expected result:function should return added question.
	 * 
	 * @author rohit
	 */
	

	public void testGetQuestionByIdPositiveCase() { // test data
		int testUserId = -1;
		try {
			// create a test user
			testUserId = CommonJUnit.createTestUser();
			// create a question
			Question newQuestion = new Question();
			newQuestion.setQuestion("testtesttest");
			newQuestion.setTitle("test");
			newQuestion.setUserId(testUserId);
			//create a tag
			Tag tag=new Tag();
			tag.setTagName("testTag");
			// add question to database with tags(mapping in question tag table.)
			int questionId = CommonJUnit.insertTestData(newQuestion);
			//run service class method.
			Question question=QuestionService.getQuestionById(questionId);
			System.out.println(question);
			//test question 
			assertTrue(newQuestion.getQuestion().equals(question.getQuestion()));
			assertTrue(newQuestion.getTitle().equals(question.getTitle()));
			assertEquals(newQuestion.getUserId(),question.getUserId());
		} catch (Exception e) {
			e.printStackTrace();
			fail();
		} finally {
			try {
				CommonJUnit.deleteTestDataFromQuestion_Tags(testUserId);
				CommonJUnit.deleteTestData("METACUBEGRANTH_QUESTION",
						testUserId);
				CommonJUnit.deleteTestDataFromTags("testtag");
				CommonJUnit.deleteTestUser();
			} catch (SQLException e) {
				fail();
				e.printStackTrace();
			}

		}
	}

	
}
